package com.ruoyi.user.service.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginAppUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.game.domain.GameRole;
import com.ruoyi.game.mapper.GameRoleMapper;
import com.ruoyi.server.domain.ServerInfo;
import com.ruoyi.server.mapper.ServerInfoMapper;
import com.ruoyi.user.domain.AppUserAccount;
import com.ruoyi.user.domain.dto.AppUserAccountAddDTO;
import com.ruoyi.user.domain.dto.AppUserAccountPageDTO;
import com.ruoyi.user.domain.dto.AppUserAccountUpdateDTO;
import com.ruoyi.user.mapper.AppUserAccountMapper;
import com.ruoyi.user.service.IAppUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户账号Service业务层处理
 *
 * @author ghostmen
 * @date 2024-11-02
 */
@Service
public class AppUserAccountServiceImpl extends ServiceImpl<AppUserAccountMapper, AppUserAccount> implements IAppUserAccountService {

    @Autowired
    private AppUserAccountMapper appUserAccountMapper;
    @Autowired
    private GameRoleMapper gameRoleMapper;
    @Autowired
    private ServerInfoMapper serverInfoMapper;

    /**
     * 查询用户账号
     *
     * @param accountId 用户账号主键
     * @return 用户账号
     */
    @Override
    public AppUserAccount selectAppUserAccountByAccountId(Long accountId) {
        return appUserAccountMapper.selectAppUserAccountByAccountId(accountId);
    }

    /**
     * 查询用户账号列表
     *
     * @param pageDTO 登录用户
     * @return 用户账号
     */
    @Override
    public List<AppUserAccount> selectAppUserAccountList(AppUserAccountPageDTO pageDTO) {
        return appUserAccountMapper.selectAppUserAccountList(pageDTO);
    }

    /**
     * 查询用户账号列表
     *
     * @param userId 用户id
     * @return 用户账号
     */
    @Override
    public List<AppUserAccount> userAccountList(Long userId) {
        return appUserAccountMapper.userAccountList(userId);
    }

    /**
     * 新增用户账号
     *
     * @param addDTO 用户账号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAppUserAccount(AppUser appUser, AppUserAccountAddDTO addDTO) {
        AppUserAccount appUserAccount = BeanUtil.copyProperties(addDTO, AppUserAccount.class);
        //校验数据
        verifyData(appUserAccount);
        appUserAccount.setAccountId(IdWorker.getId());
        appUserAccount.setCreateTime(DateUtils.getNowDate());
        appUserAccount.setCreateBy(appUser.getUserName());
        appUserAccount.setUserId(appUser.getUserId());
        return appUserAccountMapper.insertAppUserAccount(appUserAccount);
    }

    /**
     * 校验数据
     *
     * @param appUserAccount
     */
    private void verifyData(AppUserAccount appUserAccount) {
        GameRole gameRole = gameRoleMapper.selectGameRoleByRoleId(appUserAccount.getRoleId());
        if (Objects.isNull(gameRole)) {
            throw new ServiceException("该角色不存在");
        }
        ServerInfo serverInfo = serverInfoMapper.selectServerInfoByServerId(appUserAccount.getServerId());
        if (Objects.isNull(serverInfo)) {
            throw new ServiceException("该服务器不存在");
        }
        appUserAccount.setRoleName(gameRole.getRoleName());
        appUserAccount.setServer(serverInfo.getServer());
    }

    /**
     * 修改用户账号
     *
     * @param updateDTO 用户账号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateAppUserAccount(AppUser appUser, AppUserAccountUpdateDTO updateDTO) {
        AppUserAccount appUserAccount = BeanUtil.copyProperties(updateDTO, AppUserAccount.class);
        //校验数据
        verifyData(appUserAccount);
        appUserAccount.setUpdateTime(DateUtils.getNowDate());
        appUserAccount.setUpdateBy(appUser.getUserName());
        return appUserAccountMapper.updateAppUserAccount(appUserAccount);
    }

    /**
     * 批量删除用户账号
     *
     * @param accountIds 需要删除的用户账号主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAppUserAccountByAccountIds(Long[] accountIds) {
        return appUserAccountMapper.deleteAppUserAccountByAccountIds(accountIds);
    }

    /**
     * 删除用户账号信息
     *
     * @param accountId 用户账号主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAppUserAccountByAccountId(Long accountId) {
        return appUserAccountMapper.deleteAppUserAccountByAccountId(accountId);
    }

    @Override
    public AppUserAccount getUserAccountByUserIdAndAccountId(Long userId, Long accountId) {
        return appUserAccountMapper.getUserAccountByUserIdAndAccountId(userId, accountId);
    }
}
